<?php

class Zhishidi_Dailydeals_Adminhtml_Zhishidi_DailydealsController extends Mage_Adminhtml_Controller_Action {

    public function indexAction() {
        $this->loadLayout();
        $this->getLayout()->getBlock('head')->setTitle(Mage::helper('zhishidi_dailydeals')->__('Manage Daily Deals'));
        $this->renderLayout();
    }

    public function gridAction() {
        $this->loadLayout();
        $this->getResponse()->setBody($this->getLayout()->createBlock('zhishidi_dailydeals/adminhtml_dailydeals_grid')->toHtml());
    }

    public function saveAction() {
        if ($data = $this->getRequest()->getPost()) {
            try {
                $model = Mage::getModel('zhishidi_dailydeals/deals');
                $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $data['product_sku']);
              
                if (!$product) {
                    Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_dailydeals')->__('Product sku(%s) does not exists', $data['product_sku']));
                    Mage::getSingleton('adminhtml/session')->setDailydealsData($data);
                    $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                    return false;
                }
                $data['product_id'] = $product->getId();


                if (!isset($data['deal_created'])) {
                    $data['deal_created'] = date('Y-m-d H:i:s', Mage::app()->getLocale()->storeTimeStamp());
                }


                $model->setData($data)->setId($this->getRequest()->getParam('id'));



                $model->save();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('zhishidi_dailydeals')->__('Deal was successfully saved'));
                Mage::getSingleton('adminhtml/session')->setDailydealsData(false);

                if ($this->getRequest()->getParam('back')) {
                    $this->_redirect('*/*/edit', array('id' => $model->getId()));
                    return;
                }
                $this->_redirect('*/*/');
                return true;
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                Mage::getSingleton('adminhtml/session')->setDailydealsData($data);
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
                return false;
            }
        }
    }

    public function massStatusAction() {
        $deals = $this->getRequest()->getParam('mass_action_deals');

        if (!is_array($deals)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_dailydeals')->__('Please select deals'));
        } else {
            try {
                foreach ($deals as $deal) {
                    $deal = Mage::getSingleton('zhishidi_dailydeals/deals')
                                    ->load($deal)
                                    ->setDeal_status($this->getRequest()->getParam('deal_status'))->save();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                        Mage::helper('zhishidi_dailydeals')->__(
                                'Total of %d Deal(s) were successfully updated', count($deals)
                        )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/');
    }

    public function newAction() {
        $this->_forward('edit');
    }

    public function massDeleteAction() {
        $deals = $this->getRequest()->getParam('mass_action_deals');

        if (!is_array($deals)) {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_dailydeals')->__('Please select deals'));
        } else {
            try {
                foreach ($deals as $deal) {
                    $deal = Mage::getModel('zhishidi_dailydeals/deals')->load($deal);
                    $deal->delete();
                }
                Mage::getSingleton('adminhtml/session')->addSuccess(
                        Mage::helper('zhishidi_dailydeals')->__(
                                'Total of %d Deal(s) were successfully deleted', count($deals)
                        )
                );
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
            }
        }
        $this->_redirect('*/*/');
    }

    public function deleteAction() {
        if ($this->getRequest()->getParam('id') > 0) {
            try {
                $model = Mage::getModel('zhishidi_dailydeals/deals');

                $model->setId($this->getRequest()->getParam('id'))
                        ->delete();

                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('adminhtml')->__('Deal was successfully deleted'));
                $this->_redirect('*/*/');
            } catch (Exception $e) {
                Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
                $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
            }
        }
        $this->_redirect('*/*/');
    }

    public function editAction() {

        $id = $this->getRequest()->getParam('id');
        $model = Mage::getModel('zhishidi_dailydeals/deals')->load($id);

        if ($model->getId() || $id == 0) {
            $data = Mage::getSingleton('adminhtml/session')->getDailydealsData(true);
            if (!empty($data)) {
                $model->setData($data);
            }
            if ($model->getProduct_id()) {
                $product = Mage::getModel("catalog/product")->load($model->getProduct_id());
                $model->setProduct_sku($product->getSku());
            }

            Mage::register('dailydeals_data', $model);

            $this->loadLayout();



            $this->_setActiveMenu('catalog/zhishidi_dailydeals');



            #$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item Manager'), Mage::helper('adminhtml')->__('Item Manager'));
            #$this->_addBreadcrumb(Mage::helper('adminhtml')->__('Item News'), Mage::helper('adminhtml')->__('Item News'));

            $this->getLayout()->getBlock('head')->setCanLoadExtJs(true);
            $this->getLayout()->getBlock('head')->setTitle(Mage::helper('zhishidi_dailydeals')->__('Manage Daily Deals'));
            #$this->_addContent($this->getLayout()->createBlock('zhishidi_dailydeals/adminhtml_dailydeals_edit'));
            $this->_addContent($this->getLayout()->createBlock('zhishidi_dailydeals/adminhtml_dailydeals_edit'))->_addLeft($this->getLayout()->createBlock('zhishidi_dailydeals/adminhtml_dailydeals_edit_tabs'));

            $this->renderLayout();
        } else {
            Mage::getSingleton('adminhtml/session')->addError(Mage::helper('zhishidi_dailydeals')->__('Daily deal does not exist'));
            $this->_redirect('*/*/');
        }
    }

}

